/**
 * 
 */
package br.srv.full.virtual.faces.beanutils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.srv.full.virtual.entities.auth.Role;

/**
 * @author Delfino
 * 
 */
public class RoleConverter implements Converter {

	private final Log log = LogFactory.getLog(getClass());

	{log.info("Instanciado!");}
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
	 *      javax.faces.component.UIComponent, java.lang.String)
	 */
	@Override
	public Object getAsObject(FacesContext p_arg0, UIComponent p_arg1, String p_arg2) {
		return new Role(p_arg2);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
	 *      javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public String getAsString(FacesContext p_arg0, UIComponent p_arg1, Object p_arg2) {
		if (p_arg2 instanceof Role) {
			Role l_role = (Role) p_arg2;
			return l_role.getName();
		}else{
			throw new RuntimeException("Este objeto n'ao 'e um Role: "+p_arg0);
		}

	}

}
